package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.JxcConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.JxcBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.JxcFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * jxc 消息消费者
 * @author 魏荣杰
 * @date 2019/2/25 14:27
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(JxcConsumerBinder.class)
public class JxcListener {
    private static Logger logger = LoggerFactory.getLogger(JxcListener.class);

    @Resource
    private JxcFeignClient jxcFeignClient;

    @StreamListener(value = JxcBinderConstant.SUPPLIER_INPUT)
    public void supplierReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case SUPPLIER_ADD_PUSH:
                    response = jxcFeignClient.supplierAdd(message);
                    break;
                case SUPPLIER_TEAM_ADD_DEL_MAIN_PUSH:
                    response = jxcFeignClient.supplierTeamMain(message);
                    break;
                case SUPPLIER_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = jxcFeignClient.supplierTeamSynergetic(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("JxcListener.supplierReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("JxcListener.supplierReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = JxcBinderConstant.PURCHASE_INPUT)
    public void purchaseReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case WAIT_PURCHASE_PUSH:
                    response = jxcFeignClient.waitPurchasePush(message);
                    break;
                case PURCHASE_SUCCESS_PUSH:
                    response = jxcFeignClient.purchaseSuccessPush(message);
                    break;
                case PURCHASE_TEAM_ADD_DEL_MAIN_PUSH:
                    response = jxcFeignClient.purchaseTeamMain(message);
                    break;
                case PURCHASE_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = jxcFeignClient.purchaseTeamSynergetic(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("JxcListener.purchaseReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("JxcListener.purchaseReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = JxcBinderConstant.OUTSTOCK_INPUT)
    public void outstockReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case WAIT_OUTSTOCK_PUSH:
                    response = jxcFeignClient.waitOutstockPush(message);
                    break;
                case OUTSTOCK_SUCCESS_PUSH:
                    response = jxcFeignClient.outstockSuccessPush(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("JxcListener.outstockReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("JxcListener.outstockReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = JxcBinderConstant.INSTOCK_INPUT)
    public void instockReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case WAIT_INSTOCK_PUSH:
                    response = jxcFeignClient.waitInstockPush(message);
                    break;
                case INSTOCK_SUCCESS_PUSH:
                    response = jxcFeignClient.instockSuccessPush(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("JxcListener.instockReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("JxcListener.instockReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = JxcBinderConstant.BOM_INPUT)
    public void bomReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case BOMBILL_TEAM_ADD_DEL_MAIN_PUSH:
                    response = jxcFeignClient.bomTeamMain(message);
                    break;
                case BOMBILL_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = jxcFeignClient.bomTeamSynergetic(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("JxcListener.bomReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("JxcListener.bomReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = JxcBinderConstant.PRODUCTION_ORDER_INPUT)
    public void productionOrderReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case PRODUCTION_ORDER_TEAM_ADD_DEL_MAIN_PUSH:
                    response = jxcFeignClient.productionOrderTeamMain(message);
                    break;
                case PRODUCTION_ORDER_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = jxcFeignClient.productionOrderTeamSynergetic(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("JxcListener.productionOrderReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("JxcListener.productionOrderReceive Exception, type=" + type, e);
        }
    }
}
